<html lang="en">
<head>
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
	
	
<head>
<body>
	<script src="http://code.highcharts.com/highcharts.js"></script>
	<script src="http://code.highcharts.com/modules/exporting.js"></script>
	<script src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript" src="/javascripts/regression.js"></script>
	<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

	<div id="json"></div>

	<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>-->
<script type="text/javascript">
Date.prototype.formatMMDDYYYY = function(){
    return this.getMonth() + 
    "/" +  this.getDate() +
    "/" +  this.getFullYear();
}
var chart;
$(document).ready(function() {
	//var url = 'http://app.strava.com/api/v1/segments/633807/efforts?athleteId=10749&callback=?';
	//$.getJSON(url, function(data) {
  	//	console.log('Load was performed.');
  	//	$('#json').html(data.efforts[0]);
	//});
	var json = <%- json %>;

	var categories = _.map(json.efforts, function(effort) {
		return new Date(effort.startDateLocal);
	});

/*
[{
            type: 'column',
            name: 'Jane',
            data: [3, 2, 1, 3, 4]
        }
{
            type: 'spline',
            name: 'Average',
            data: [3, 2.67, 3, 6.33, 3.33]
        }*/
	//var series = _.map(json.efforts, function(effort) {
	//	return {type:'column', name: effort.startDateLocal, data: []};
	//});

	// todo: display best time on graph somewhere

	var seriesData = _.map(json.efforts, function(effort) {
		return [new Date(effort.startDateLocal).getTime(), effort.elapsedTime];
	});


	var series = { type: 'spline', name:json.segment.name, data: seriesData };
	//console.log(series);

	var trendLine = {
		type: 'line',
		marker: { enabled: false },
		/* function returns data for trend-line */
		data: (function() {
		  return fitData(seriesData).data;
		})()
	};

    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container'
        },
        title: {
            text: 'Combination chart'
        },
        yAxis: {
			title: {
                    text: 'Time'
                },
            labels: {
    	        formatter: function() {
    	        	var time = this.value;
    	        	var minutes = Math.floor(time / 60);
    	        	var seconds = time - minutes * 60;
    	        	//var hours = Math.floor(time / 3600);
					//time = time - hours * 3600;

        	        return minutes + ":" + seconds
            	}
	        }
        },
        xAxis: {
        	type: 'datetime',
            dateTimeLabelFormats: { // don't display the dummy year
                day: '%e of %b'
            },
        	title: {
                    text: 'Effort Date'
            }/*,
            labels: {
                formatter: function() {
                    return this.value.formatMMDDYYYY()
                }
            },
            categories: categories*/
        },
        tooltip: {
            formatter: function() {
                var s;
                if (this.point.name) { // the pie chart
                    s = ''+
                        this.point.name +': '+ this.y +' fruits';
                } else {
                    s = ''+
                        this.x  +': '+ this.y;
                }
                return s;
            }
        },
        labels: {
            items: [{
                html: 'Total fruit consumption',
                style: {
                    left: '40px',
                    top: '8px',
                    color: 'black'
                }
            }]
        },
        series: [series,
        trendLine]

        /*[{
            type: 'column',
            name: 'Jane',
            data: [3, 2, 1, 3, 4]
        }, {
            type: 'column',
            name: 'John',
            data: [2, 3, 5, 7, 6]
        }, {
            type: 'column',
            name: 'Joe',
            data: [4, 3, 3, 9, 0]
        }*//*, {
            type: 'spline',
            name: 'Average',
            data: [3, 2.67, 3, 6.33, 3.33]
        }*//*, {
            type: 'pie',
            name: 'Total consumption',
            data: [{
                name: 'Jane',
                y: 13,
                color: '#4572A7' // Jane's color
            }, {
                name: 'John',
                y: 23,
                color: '#AA4643' // John's color
            }, {
                name: 'Joe',
                y: 19,
                color: '#89A54E' // Joe's color
            }],
            center: [100, 80],
            size: 100,
            showInLegend: false,
            dataLabels: {
                enabled: false
            }
        }]*/
    });
});

</script>
​</body>
</html>